<?php


namespace app\index\controller;


use app\model\Boutcardorder;
use app\model\Goods;
use app\model\Member;
use app\model\Order;
use app\model\Paymethod;

class BoutcardorderController extends Base
{
    //订单状态，0:未支付，1:待使用，2:使用中，3:已使用,4:待退款，5:已退款,6:已取消
    public function myorder(){
        $uniacid = uniacid();
        $status = input('post.status');
        $query = Boutcardorder::where(['uniacid' => $uniacid]);

        $query->with(['paymethod']);

        $Membermob = new Member;
        $Member = $Membermob->getUserByWechat();

        $query->where(['uid' => $Member['id']]);


        switch ($status) {
            case 1:
                $query->where(['status'=>0]);
                break;
            case 2:
                $query->where(['status'=>1]);
                break;
            case 3:
                $query->where(['status'=>2]);
                break;
            case 4:
                $query->where(['status'=>3]);
                break;
            case 5:
                $query->whereIn('status',[4,5,6]);
                break;
            default:
                break;
        }

        $res = $query->order('id desc')
            ->paginate(getpage())
            ->toArray();

        $data['errno']=0;
        $data['data'] = $res;
        return $this->json($data);
    }

    public function create(){
        $ordermod = new Boutcardorder();
        $payment = input('post.paymentType');
        $sid = input('post.sid/d');

        $goods_id = input('post.goods_id');
        $cat_id = input('post.cat_id');
        $price = input('post.price');


        $order['sid'] = $sid;
        $order['goods_id'] = $goods_id;
        $order['price'] = $price;
        $order['cat_id'] = $cat_id;
        $order['ptype'] = 2;

        //支付方式
        if (!empty($payment)) {
            $order['payment_code'] = $payment;

            $paymentdata = Paymethod::where(['code' => $payment, 'uniacid' => uniacid()])->find();

            $order['pay_method_id'] = $paymentdata->id;
        }

        $order['uid'] = UID();

        $data = $ordermod->add_order($order);

        return $this->json(['errno'=>0,'data'=>$data]);
    }

    //直接使用余额支付
    public function pay()
    {
        $uid = UID();
        $payPwd = input('post.payPwd');
        if(empty($payPwd)){
            return $this->json(['errno'=>1,'message'=>'请输入支付密码']);
        }

        $memberinfo = Member::find($uid);
        if(empty($memberinfo)){
            return $this->json(['errno'=>1,'message'=>'用户不存在']);
        }

        if($memberinfo->pay_pwd != $payPwd){
            return $this->json(['errno'=>1,'message'=>'支付密码错误']);
        }


        $orderid = input('post.orderid');
        $orderinfo = Boutcardorder::find($orderid);
        $errno = 0;
        if (!empty($orderinfo)) {
            $money = $orderinfo['price'];
            $Member = Member::find(UID());
            if (!empty($Member) && ($Member->balance >= $money)) {
                Member::where('id', UID())
                    ->dec('balance', $money)
                    ->update();

                Boutcardorder::update(['id' => $orderid, 'pay_time' => time(), 'payment_code' => 'balance_pay','status'=>1]);
            } else {
                $errno = 1;
                $message = '余额不足';
            }
        } else {
            $errno = 1;
            $message = '订单不存在';
        }

        return $this->json(['msg' => $message, 'message' => $message, 'errno' => $errno, 'data' => []]);
    }

    public function statusName(){
        $data[0]['id'] = 0;
        $data[0]['uniacid'] = 0;
        $data[0]['name'] = '全部';
        $data[0]['status'] = 1;

        $data = [
            ['id'=>0,'name'=>'全部'],
            ['id'=>1,'name'=>'待支付'],
            ['id'=>2,'name'=>'待使用'],
            ['id'=>3,'name'=>'使用中'],
            ['id'=>4,'name'=>'已使用'],
            ['id'=>5,'name'=>'售后'],
        ];
        //0:未支付，1:待使用，2:使用中，3:已使用, 4:售后
        return $this->json(['errno'=>0,'data'=>$data]);
    }

    public function cancel(){

        $orderid = input('post.orderid');

        Boutcardorder::cancel_order($orderid);

        $data['orderid'] = $orderid;

        return $this->json(['data' => $data]);

    }

    public function detail(){
        $orderid = input('post.orderid');

        $data = Boutcardorder::find($orderid);
        if(!empty($data)){
            $data = $data->toArray();
        }else{
            $data = [];
        }
        return $this->json(['data' => $data]);

    }
}
